package com.saa.common.utils;

import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;


import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;


public class DataUtils {
    /**
     * @description 将日期本地化
     * @invoked
     * @author dongzhijia 2017年7月19日
     * @param
     * @return Date
     */
    public static String toLocalDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(date);
        return formatDate;
    }

    /**
     * 把日期类型格式化成字符串
     * @param date
     * @param format
     * @return
     */
    public static String convert2String(Date date, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            return formater.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * @description 获取本地化的日期字符串
     * @invoked
     * @author dongzhijia 2017年7月19日
     * @param
     * @return Date
     */
    public static String getLocalDateString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(new Date());
        return formatDate;
    }
    /**
     * @description 获取本地化的日期
     * @invoked
     * @author dongzhijia 2017年7月19日
     * @param
     * @return Date
     * @throws ParseException
     */
    public static Date getLocalDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(new Date());
        try {
            return sdf.parse(formatDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new Date();
        }
    }
    /**
     * @description 获取两个日期相距多少天，不满一天向上取整，结果为绝对值
     * @invoked
     * @author dongzhijia 2017年7月22日
     * @param
     * @return int
     */
    public static int getDistanceDays(Date laterDate, Date earlierDate) {
        long laterTime = laterDate.getTime();
        long earlierTime = earlierDate.getTime();
        double distanceTime = Math.abs(laterTime - earlierTime);
        if(distanceTime == 0) {
            return 0;
        }
        int days = (int) Math.ceil(distanceTime / (1000 * 60 * 60 *24));
        return days;
    }
    /**
     * 将Date类转换为XMLGregorianCalendar zhanghui20170921
     * @param date
     * @return
     */
    public static XMLGregorianCalendar dateToXmlDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DatatypeFactory dtf = null;
        XMLGregorianCalendar dateType = null;
        try {
            dtf = DatatypeFactory.newInstance();
            dateType = dtf.newXMLGregorianCalendar();
            dateType.setYear(cal.get(Calendar.YEAR));
            //由于Calendar.MONTH取值范围为0~11,需要加1
            dateType.setMonth(cal.get(Calendar.MONTH)+1);
            dateType.setDay(cal.get(Calendar.DAY_OF_MONTH));
            dateType.setHour(cal.get(Calendar.HOUR_OF_DAY));
            dateType.setMinute(cal.get(Calendar.MINUTE));
            dateType.setSecond(cal.get(Calendar.SECOND));
        } catch (DatatypeConfigurationException e) {

        }

        return dateType;
    }

    /**
     * 将XMLGregorianCalendar转换为Date zhanghui20170921
     * @param cal
     * @return
     */
    public static Date xmlDate2Date(XMLGregorianCalendar cal){
        return cal.toGregorianCalendar().getTime();
    }
    public static int getDatePoor(Date before, Date after) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateFirst = dateFormat.format(before);
        String dateLast = dateFormat.format(after);
        int dateFirstIntVal = Integer.parseInt(dateFirst);
        int dateLastIntVal = Integer.parseInt(dateLast);
        if (dateFirstIntVal > dateLastIntVal) {
            return 1;
        } else if (dateFirstIntVal < dateLastIntVal) {
            return -1;
        }
        return 0;
	}
	
	public static String getStartDate(String startDate) {
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s");
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(startDate, inputFormat).format(outputFormat);
    }
	
}
